package com.beiing.tianshuai.tianshuai.dongtai.presenter;

import android.util.Log;

import com.beiing.tianshuai.tianshuai.dongtai.CallBack;
import com.beiing.tianshuai.tianshuai.dongtai.model.DynamicRelayDetailModel;
import com.beiing.tianshuai.tianshuai.dongtai.model.DynamicRelayDetailModelImpl;
import com.beiing.tianshuai.tianshuai.dongtai.view.DynamicRelayDetailViewImpl;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddRelayCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddReplyBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicDeleteBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicRelayDetailBean;
import com.beiing.tianshuai.tianshuai.entity.FollowBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.rx.MsgEvent;
import com.beiing.tianshuai.tianshuai.rx.RxBus;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

/**
 * Created by magikliu on 2017/8/24.
 */

public class DynamicRelayDetailPresenter implements DynamicRelayDetailPresenterImpl {

    private DynamicRelayDetailViewImpl mView;
    private DynamicRelayDetailModelImpl mModel;

    public DynamicRelayDetailPresenter(DynamicRelayDetailViewImpl mView) {
        this.mView = mView;
        this.mModel = new DynamicRelayDetailModel();
    }

    @Override
    public void getRelayDetail(String id, String uid, int type, int page, int num, boolean isLoadMore) {
//        mView.showProgress();
        mModel.getRelayDetail(id, uid, type, page, num, new CallBack<DynamicRelayDetailBean>() {

            @Override
            public void result(DynamicRelayDetailBean bean) {
                mView.hideProgress();
                if (bean.getCode() == 200){
                    mView.showDynamicRelayDetail(bean);
                } else {
                    mView.showDynamicNotExist();
                }
            }

            @Override
            public void error(Throwable e) {
                LogUtils.i(e.toString(), "error");
                mView.showError(0);
            }
        });
    }

    @Override
    public void addComment(final String rid, final String uid, String did, String to_uid, final String content, final int dynamic_position) {
        mModel.addComment(rid, uid, did, to_uid, content, new CallBack<DynamicAddRelayCommentBean>() {
            @Override
            public void result(DynamicAddRelayCommentBean bean) {
//                mView.refreshComment();
                getRelayDetail(rid, uid, 0, 1, 20, false);
                RxBus.getInstanceBus().post(new MsgEvent<>("dynamicRelay_detail_comment", content, dynamic_position));
            }

            @Override
            public void error(Throwable e) {
                LogUtils.e(e.getMessage(), "addComment");
                mView.showError(1);
            }
        });
    }

    @Override
    public void addReply(final String rid, String cid, final String form_uid, String to_uid, String content, String registration_id) {
        mModel.addReply(rid, cid, form_uid, to_uid, content, registration_id, new CallBack<DynamicAddReplyBean>() {
            @Override
            public void result(DynamicAddReplyBean bean) {
                mView.refreshComment();
                getRelayDetail(rid, form_uid, 0, 1, 20, false);
            }

            @Override
            public void error(Throwable e) {
                Log.i("回复",e.toString());
                mView.showError(2);
            }
        });
    }

    @Override
    public void addPraise(final String uid, final String rid, String did, String to_uid, final int dynamic_position) {
        mModel.addPraise(uid, rid, did, to_uid, new CallBack<DynamicPraiseBean>() {

            @Override
            public void result(DynamicPraiseBean bean) {
                mView.showPraiseResult(bean);
                RxBus.getInstanceBus().post(new MsgEvent<>("转发点赞", bean.getCode(), dynamic_position));
            }

            @Override
            public void error(Throwable e) {
                mView.showError(3);
            }
        });
    }

    @Override
    public void deleteComment(final String uid, final String id, final String rid, final int position, final int dynamic_position) {
        mModel.deleteComment(uid, id, rid, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {
                mView.deleteComment(position);
                getRelayDetail(rid, uid, 0, 1, 20, false);
                RxBus.getInstanceBus().post(new MsgEvent<>("dynamicRelay_detail_comment_delete", id, dynamic_position));
            }

            @Override
            public void error(Throwable e) {
                mView.showError(4);
            }
        });
    }

    @Override
    public void isFollowed(String fromUid, String toUid) {
        mModel.isFollowed(fromUid, toUid, new CallBack<FollowBean>() {

            @Override
            public void result(FollowBean bean) {
                mView.showFollowed(bean);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void setFollowed(final String fromUid, final String toUid) {
        mModel.setFollowed(fromUid, toUid, new CallBack<FollowResultBean>() {

            @Override
            public void result(FollowResultBean bean) {
                isFollowed(fromUid, toUid);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void deleteReply(String uid, String id, final int position, final int childPosition) {
        mModel.deleteReply(uid, id, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {
                mView.deleteReply(position, childPosition);
            }

            @Override
            public void error(Throwable e) {
                mView.showError(5);
            }
        });
    }

    @Override
    public void praiseComment(String uid, String cid, String did, String to_uid, String rid, final int dynamic_position) {
        mModel.praiseComment(uid, cid, did, to_uid, rid, new CallBack<DynamicPraiseBean>() {

            @Override
            public void result(DynamicPraiseBean bean) {
                mView.showCommentPraiseResult(bean, dynamic_position);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }
}
